import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:material_collection_app/utils/dio_utils.dart';
import 'package:material_collection_app/utils/utils.dart';
import 'package:material_collection_app/widget/chart/bar_chart.dart';
import 'package:material_collection_app/widget/chart/line_chart2.dart';
import 'package:material_collection_app/widget/chart/ring_chart2.dart';
import 'package:material_collection_app/widget/common/top_bar.dart';


class DataStatisticsPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => MyState();
}

class TimeSeriesSales {
  final DateTime time;
  final int sales;

  TimeSeriesSales(this.time, this.sales);
}

class MyState extends State<DataStatisticsPage> {
  Map storeInfo = {};
  List storeCounts = [0,0,0];
  List fiveDays = [0,0,0];
  List fiveChart = [];
  List businessData = [];
  int businessTotal = 0; 

  @override
  void initState() {
    super.initState();
    _getData();
  }

  // 获取数据
  _getData() {
    DioUtils.request("/Statistics/getDataStatistics",
      context: context,
      method: "GET",
      params: {},
      onSuccess: (res) {
        if(res['code'] == 1) {
          Map store = res['data']['store'];
          Map lastDay = res['data']['lastDay'];
          Map lastDayList = res['data']['lastDay']['lastDayList'];
          List transfer = res['data']['transfer'];

          storeCounts[0] = store['total'];
          storeCounts[1] = store['occupy'];
          storeCounts[2] = store['empty'];

          fiveDays[0] = lastDay['doing'];
          fiveDays[1] = lastDay['done'];
          fiveDays[2] = lastDay['launch'];


          lastDayList.values.forEach((val){
            fiveChart.add(val);
          });

          for(var i=0; i<transfer.length; i++) {
            var item = transfer[i];
            businessTotal += item['sum'];
          }

          businessData = res['data']['transfer'];
          
          setState(() {
            storeInfo = store;
          });
        }else {
          Utils.showText(text: res['msg']);
        }
      },
      onError: (error) {
        // Toast.show(error);
      }
    );
  }

  Widget _buildLabel(title, color) {
    return Container(
      margin: EdgeInsets.only(left: 25.w),
      child: Row(
        children: <Widget>[
          Container(
            width: 36.w,
            height: 14.w,
            margin: EdgeInsets.only(right: 14.w),
            decoration: BoxDecoration(
              color: Utils.hexToColor(color),
              // #F1F1F1
              borderRadius: BorderRadius.circular(7.w)
            ),
          ),
          Text(title, style: TextStyle(
            color: Utils.hexToColor('#656565'),
            fontSize: 12
          ))
        ],
      ),
    );
  }

  Widget _buildGridCard(title, number, [showBorder = true]) {
    return Container(
      width: 236.w,
      decoration: BoxDecoration(
        border: Border(right: BorderSide(color: showBorder ? Utils.hexToColor('#F2F2F2') : Colors.white))
      ),
      child: Column(
        children: <Widget>[
          Text(title, style: TextStyle(
            color: Utils.hexToColor('#C3C1C7'),
            fontSize: 14
          )),
          Container(
            margin: EdgeInsets.only(top: 20.w),
            child: Text(number, style: TextStyle(
              color: Utils.hexToColor('#297BFF'),
              fontSize: 21,
              fontWeight: FontWeight.w500
            )),
          )
        ],
      ),
    );
  }
  

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false);

    return new Scaffold(
      appBar: new TopBar(
        title: '数据统计', 
      ),
      body: SingleChildScrollView(
        child: Container(
          // color: Colors.red,
          color: Utils.hexToColor('#F5F6FA'),
          child: Column(
            children: <Widget>[
              Stack(
                children: <Widget>[
                  Align(
                    child: Container(
                      height: 110.w,
                      color: Utils.hexToColor('#297CFF'),
                      padding: EdgeInsets.all(10),
                    ),
                  ),
                  Align(
                    child: Container(
                      width: 710.w,
                      height: 850.w,
                      margin: EdgeInsets.only(top: 20.w),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(20.w)
                      ),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Container(
                            padding: EdgeInsets.fromLTRB(37.w, 28.w, 37.w, 0),
                            child: Text('仓储库位分析', style: TextStyle(
                              color: Utils.hexToColor('#333333'),
                              fontSize: 16,
                              fontWeight: FontWeight.bold
                            ))
                          ),
                          Container(
                            margin: EdgeInsets.only(top: 40.w),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: <Widget>[
                                _buildGridCard('仓库总库位', storeCounts[0].toString()),
                                _buildGridCard('已使用库位', storeCounts[1].toString()),
                                _buildGridCard('未使用库位', storeCounts[2].toString(), false),
                              ],
                            ),
                          ),
                          Container(
                            width: double.infinity,
                            height: 580.w,
                            padding: EdgeInsets.fromLTRB(15.w, 60.w, 0, 0),
                            child: BarChartSample(
                              data: storeInfo['chart']
                            ),
                          ),
                        ],
                      )
                    ),
                  ),
                ],
              ),
              Container(
                width: 710.w,
                height: 620.w,
                margin: EdgeInsets.only(top: 20.w),
                padding: EdgeInsets.fromLTRB(37.w, 28.w, 37.w, 0),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(20.w)
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Container(
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: <Widget>[
                          Text('业务处置情况', style: TextStyle(
                            color: Utils.hexToColor('#333333'),
                            fontSize: 16,
                            fontWeight: FontWeight.bold
                          ))
                        ]
                      )
                    ),
                    Container(
                      height: 400.w,
                      margin: EdgeInsets.only(top: 30.w),
                      child: RingChart2(
                        data: businessData,
                        total: businessTotal,
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.only(top: 40.w),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          _buildLabel('拍卖', '#FFAF07'),
                          _buildLabel('销毁', '#7777FF'),
                          _buildLabel('取样', '#1FAFF7'),
                          _buildLabel('移交', '#1BC75D'),
                        ],
                      ),
                    )
                  ],
                )
              ),
              Container(
                width: 710.w,
                height: 770.w,
                margin: EdgeInsets.only(top: 20.w),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(20.w)
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Container(
                      padding: EdgeInsets.fromLTRB(37.w, 28.w, 37.w, 0),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: <Widget>[
                          Text('近5天业务数据图', style: TextStyle(
                            color: Utils.hexToColor('#333333'),
                            fontSize: 16,
                            fontWeight: FontWeight.bold
                          ))
                        ]
                      )
                    ),
                    Container(
                      margin: EdgeInsets.only(top: 45.w),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: <Widget>[
                          _buildGridCard('未处置工单数', fiveDays[0].toString()),
                          _buildGridCard('已办结工单数', fiveDays[1].toString()),
                          _buildGridCard('业务处置总单数', fiveDays[2].toString(), false),
                        ],
                      ),
                    ),
                    Container(
                      height: 200.0,
                      padding: EdgeInsets.fromLTRB(15.w, 30.w, 0.w, 0),
                      margin: EdgeInsets.only(top: 20.w),
                      child: LineChartSample2(
                        data: fiveChart,
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.only(top: 15.w),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          _buildLabel('近五天业务处置数数据图', '#297BFF'),
                          // _buildLabel('未处置工单数', '#297BFF'),
                          // _buildLabel('已办结工单数', '#FFAF07'),
                        ]
                      )
                    )
                  ]
                )
              ),
              Container(
                height: 40.w,
              ),
            ],
          ),
        ),
      )
    );
  }
}